﻿using System;
using System.Drawing;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using WmsApp.Factory;
using WmsApp.StaticData;
using WmsApp.WmsService;

namespace WmsApp.Lookup
{
    public class LookupBind
    {
        public static void BindingLookUpEditBillState(LookUpEdit lookUpEdit)
        {
            var itemLookUpEdit = lookUpEdit;
            itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("Code", "单据类型代码"),
                                                               new LookUpColumnInfo("Name", "单据类型名称")
                                                           });
            itemLookUpEdit.Properties.DisplayMember = "Name";
            itemLookUpEdit.Name = "LookUpEditBillState";
            itemLookUpEdit.Properties.ValueMember = "Code";
            itemLookUpEdit.Properties.NullText = "";
            itemLookUpEdit.Properties.DataSource = WmsServiceFactory.GetServiceClient().GetInStoreTypes();
        }

        public static void BindingLookUpEditIsAlive(GridView gv, GridColumn gc)
        {
            var repositoryItemLookUpEditAliveState = new RepositoryItemLookUpEdit {AutoHeight = false};

            repositoryItemLookUpEditAliveState.Columns.AddRange(new[]
                                                                    {
                                                                        new LookUpColumnInfo("Code", "代码"),
                                                                        new LookUpColumnInfo("Name", "名称")
                                                                    });
            repositoryItemLookUpEditAliveState.DisplayMember = "Name";
            repositoryItemLookUpEditAliveState.Name = "repositoryItemLookUpEditAliveState";
            repositoryItemLookUpEditAliveState.ValueMember = "Code";
            repositoryItemLookUpEditAliveState.NullText = "";
            gv.GridControl.RepositoryItems.AddRange(
                new RepositoryItem[] {repositoryItemLookUpEditAliveState});
            repositoryItemLookUpEditAliveState.DataSource = AliveState.GetAliveStateList();
            gc.ColumnEdit = repositoryItemLookUpEditAliveState;
        }

        public static void BindingLookUpEditUnit(GridView gv, GridColumn gc, GridColumn valueColumn)
        {
            var repositoryItemLookUpEdit = new RepositoryItemLookUpEdit {AutoHeight = false};

            repositoryItemLookUpEdit.Columns.AddRange(new[]
                                                          {
                                                              new LookUpColumnInfo("UnitCode", "代码"),
                                                              new LookUpColumnInfo("UnitName", "名称")
                                                          });
            repositoryItemLookUpEdit.DisplayMember = "UnitCode";
            repositoryItemLookUpEdit.Name = "repositoryItemLookUpEditUnit";
            repositoryItemLookUpEdit.ValueMember = "UnitCode";
            repositoryItemLookUpEdit.NullText = "";
            gv.GridControl.RepositoryItems.AddRange(
                new RepositoryItem[] {repositoryItemLookUpEdit});
            repositoryItemLookUpEdit.DataSource = WmsServiceFactory.GetServiceClient().GetUnits();
            gc.ColumnEdit = repositoryItemLookUpEdit;
            repositoryItemLookUpEdit.EditValueChanging += (sender, e) =>
                                                              {
                                                                  var unit =
                                                                      (Unit)
                                                                      repositoryItemLookUpEdit.
                                                                          GetDataSourceRowByKeyValue(e.NewValue);
                                                                  gv.SetRowCellValue(gv.FocusedRowHandle, valueColumn,
                                                                                     unit.UnitName);
                                                              };
        }

        public static void BindingLookUpEditSystemUser(GridView gv, GridColumn gc, GridColumn valueColumn)
        {
            var repositoryItemLookUpEdit = new RepositoryItemLookUpEdit {AutoHeight = false};
            repositoryItemLookUpEdit.TextEditStyle = TextEditStyles.Standard;
            repositoryItemLookUpEdit.Columns.AddRange(new[]
                                                          {
                                                              new LookUpColumnInfo("USERID", "代码"),
                                                              new LookUpColumnInfo("USERNAME", "名称")
                                                          });
            repositoryItemLookUpEdit.DisplayMember = "USERID";
            repositoryItemLookUpEdit.Name = "repositoryItemLookUpEdit";
            repositoryItemLookUpEdit.ValueMember = "USERID";
            repositoryItemLookUpEdit.NullText = "";
            gv.GridControl.RepositoryItems.AddRange(
                new RepositoryItem[] {repositoryItemLookUpEdit});
            repositoryItemLookUpEdit.DataSource = WmsServiceFactory.GetServiceClient().GetSystemUsers();
            gc.ColumnEdit = repositoryItemLookUpEdit;
            repositoryItemLookUpEdit.EditValueChanging += (sender, e) =>
                                                              {
                                                                  var user =
                                                                      (SystemUser)
                                                                      repositoryItemLookUpEdit.
                                                                          GetDataSourceRowByKeyValue(e.NewValue);
                                                                  gv.SetRowCellValue(gv.FocusedRowHandle, valueColumn,
                                                                                     user.USERNAME);
                                                              };
        }

        public static void BindingLookUpEditProvider(LookUpEdit lookUpEdit, TextEdit valueTextEdit)
        {
            var itemLookUpEdit = lookUpEdit;
            itemLookUpEdit.Properties.TextEditStyle = TextEditStyles.Standard;
            itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("OWNERNO", "代码"),
                                                               new LookUpColumnInfo("OWNERNAME", "名称")
                                                           });
            itemLookUpEdit.Properties.DisplayMember = "OWNERNO";
            itemLookUpEdit.Name = "LookUpEditCustomer";
            itemLookUpEdit.Properties.ValueMember = "OWNERNO";
            itemLookUpEdit.Properties.NullText = "";
            itemLookUpEdit.Properties.DataSource = WmsServiceFactory.GetServiceClient().GetCustomers();

            itemLookUpEdit.EditValueChanging += (sender, e) =>
                                                    {
                                                        var customer =
                                                            (Customer)
                                                            itemLookUpEdit.Properties.GetDataSourceRowByKeyValue(
                                                                e.NewValue);
                                                        if (customer != null)
                                                        {
                                                            if (itemLookUpEdit.DataBindings.Count > 0)
                                                            {
                                                                var type =
                                                                    itemLookUpEdit.DataBindings[0].DataSource.GetType();
                                                                var pi = type.GetProperty("ProviderName");
                                                                pi.SetValue(itemLookUpEdit.DataBindings[0].DataSource,
                                                                            customer.OWNERNAME, null);
                                                            }
                                                        }
                                                    };
        }

        public static RepositoryItemGridLookUpEdit BindingLookUpEditGoodsCodeType(
            GridView gv, GridColumn gc,
            GridColumn valueColumnName, GridColumn valueColumnOldCode,
            GridColumn valueColumnUnitCode, GridColumn valueColumnUnitName,
            GridColumn valueColumnStoreCode, GridColumn valueColumnStoreName)
        {
            var repositoryItemLookUpEdit = new RepositoryItemGridLookUpEdit
                                               {
                                                   AutoHeight = true,
                                                   PopupFormMinSize = new Size(600, 180)
                                               };

            var gridView = new GridView();
            gridView.Columns.AddRange(new[]
                                          {
                                              new GridColumn
                                                  {
                                                      FieldName = "NewGoodsCode",
                                                      Name = "新物料代码",
                                                      Caption = @"新物料代码",
                                                      Width = 120,
                                                      Visible = true,
                                                      VisibleIndex = 0
                                                  },
                                              new GridColumn
                                                  {
                                                      FieldName = "NewGoodsName",
                                                      Name = "新物料名称",
                                                      Caption = @"新物料名称",
                                                      Width = 160,
                                                      Visible = true,
                                                      VisibleIndex = 1
                                                  },
                                              new GridColumn
                                                  {
                                                      FieldName = "OldGoodsCode",
                                                      Name = "老物料代码",
                                                      Caption = @"老物料代码",
                                                      Width = 120,
                                                      Visible = true,
                                                      VisibleIndex = 2
                                                  },
                                              new GridColumn
                                                  {
                                                      FieldName = "StoreLocation",
                                                      Name = "库位",
                                                      Caption = @"库位",
                                                      Width = 100,
                                                      Visible = true,
                                                      VisibleIndex = 3
                                                  },
                                              new GridColumn
                                                  {
                                                      FieldName = "StoreTypeCode",
                                                      Name = "存货代码",
                                                      Caption = @"存货代码",
                                                      Width = 120,
                                                      Visible = true,
                                                      VisibleIndex = 4
                                                  },
                                              new GridColumn
                                                  {
                                                      FieldName = "StoreTypeName",
                                                      Name = "存货名称",
                                                      Caption = @"存货名称",
                                                      Width = 120,
                                                      Visible = true,
                                                      VisibleIndex = 5
                                                  },
                                              new GridColumn
                                                  {
                                                      FieldName = "UnitCode",
                                                      Name = "单位代码",
                                                      Caption = @"单位代码",
                                                      Width = 80,
                                                      Visible = true,
                                                      VisibleIndex = 6
                                                  },
                                              new GridColumn
                                                  {
                                                      FieldName = "UnitName",
                                                      Name = "单位名称",
                                                      Caption = @"单位名称",
                                                      Width = 80,
                                                      Visible = true,
                                                      VisibleIndex = 7
                                                  }
                                          });
            gridView.FocusRectStyle = DrawFocusRectStyle.RowFocus;
            gridView.OptionsSelection.EnableAppearanceFocusedCell = false;
            gridView.OptionsView.ShowAutoFilterRow = true;
            gridView.OptionsView.ShowGroupPanel = false;
            repositoryItemLookUpEdit.View = gridView;
            repositoryItemLookUpEdit.DisplayMember = "NewGoodsCode";
            repositoryItemLookUpEdit.Name = "repositoryItemLookUpEdit";
            repositoryItemLookUpEdit.ValueMember = "NewGoodsCode";
            repositoryItemLookUpEdit.NullText = "";
            gv.GridControl.RepositoryItems.AddRange(new RepositoryItem[] {repositoryItemLookUpEdit});
            repositoryItemLookUpEdit.DataSource = StaticObjects.GetGoodsCodeTypes();
            //Factory.WmsServiceFactory.GetServiceClient().GetGoodsCodeTypes();
            gc.ColumnEdit = repositoryItemLookUpEdit;
            repositoryItemLookUpEdit.EditValueChanging += (sender, e) =>
                                                              {
                                                                  var goodsCodeType =
                                                                      (GoodsCodeType)
                                                                      repositoryItemLookUpEdit.GetRowByKeyValue(
                                                                          e.NewValue);
                                                                  if (goodsCodeType == null)
                                                                      return;
                                                                  gv.SetRowCellValue(gv.FocusedRowHandle,
                                                                                gc,
                                                                                goodsCodeType.NewGoodsCode);

                                                                  gv.SetRowCellValue(gv.FocusedRowHandle,
                                                                                     valueColumnName,
                                                                                     goodsCodeType.NewGoodsName);
                                                                  gv.SetRowCellValue(gv.FocusedRowHandle,
                                                                                     valueColumnOldCode,
                                                                                     goodsCodeType.OldGoodsCode);

                                                                  gv.SetRowCellValue(gv.FocusedRowHandle,
                                                                                     valueColumnUnitCode,
                                                                                     goodsCodeType.UnitCode);
                                                                  gv.SetRowCellValue(gv.FocusedRowHandle,
                                                                                     valueColumnUnitName,
                                                                                     goodsCodeType.UnitName);

                                                                  //2010-11-18 added by henrik.库位
                                                                  //start add
                                                                  var gridColumnStoreLocation =
                                                                      gv.Columns.ColumnByFieldName("StoreLocation");
                                                                  if (gridColumnStoreLocation != null)
                                                                  {
                                                                      gv.SetRowCellValue(gv.FocusedRowHandle,
                                                                                         gridColumnStoreLocation,
                                                                                         goodsCodeType.StoreLocation);
                                                                  }
                                                                  //end add

                                                                  var obj = gv.GetRowCellValue(gv.FocusedRowHandle,
                                                                                               valueColumnStoreCode);
                                                                  if (obj != null &&
                                                                      !String.IsNullOrEmpty(obj.ToString()))
                                                                  {
                                                                  }
                                                                  else
                                                                  {
                                                                      gv.SetRowCellValue(gv.FocusedRowHandle,
                                                                                         valueColumnStoreCode,
                                                                                         goodsCodeType.StoreTypeCode);
                                                                      gv.SetRowCellValue(gv.FocusedRowHandle,
                                                                                         valueColumnStoreName,
                                                                                         goodsCodeType.StoreTypeName);
                                                                  }
                                                              };
            return repositoryItemLookUpEdit;
        }

        public static void BindingLookUpEditStoreType(GridView gv, GridColumn gc, GridColumn valueColumn)
        {
            var repositoryItemLookUpEdit = new RepositoryItemLookUpEdit {AutoHeight = false};

            repositoryItemLookUpEdit.Columns.AddRange(new[]
                                                          {
                                                              new LookUpColumnInfo("StoreCode", "代码"),
                                                              new LookUpColumnInfo("StoreName", "名称")
                                                          });
            repositoryItemLookUpEdit.DisplayMember = "StoreCode";
            repositoryItemLookUpEdit.Name = "repositoryItemLookUpEditUnit";
            repositoryItemLookUpEdit.ValueMember = "StoreCode";
            repositoryItemLookUpEdit.NullText = "";
            gv.GridControl.RepositoryItems.AddRange(
                new RepositoryItem[] {repositoryItemLookUpEdit});
            repositoryItemLookUpEdit.DataSource = WmsServiceFactory.GetServiceClient().GetStoreTypes();
            gc.ColumnEdit = repositoryItemLookUpEdit;
            repositoryItemLookUpEdit.EditValueChanging += (sender, e) =>
                                                              {
                                                                  var storeType =
                                                                      (StoreType)
                                                                      repositoryItemLookUpEdit.
                                                                          GetDataSourceRowByKeyValue(e.NewValue);
                                                                  gv.SetRowCellValue(gv.FocusedRowHandle, valueColumn,
                                                                                     storeType.StoreName);
                                                              };
        }

        public static void BindingLookUpEditInStoreType(LookUpEdit lookUpEdit)
        {
            var itemLookUpEdit = lookUpEdit;
            itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("InStoreCode", "入库类型代码"),
                                                               new LookUpColumnInfo("InStoreName", "入库类型名称")
                                                           });
            itemLookUpEdit.Properties.DisplayMember = "InStoreName";
            itemLookUpEdit.Name = "LookUpEditInStoreType";
            itemLookUpEdit.Properties.ValueMember = "InStoreCode";
            itemLookUpEdit.Properties.NullText = "";
            itemLookUpEdit.Properties.DataSource = WmsServiceFactory.GetServiceClient().GetInStoreTypes();
        }

        public static void BindingLookUpEditOutStoreType(LookUpEdit lookUpEdit)
        {
            var itemLookUpEdit = lookUpEdit;
            itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("OutStoreCode", "入库类型代码"),
                                                               new LookUpColumnInfo("OutStoreName", "入库类型名称")
                                                           });
            itemLookUpEdit.Properties.DisplayMember = "OutStoreName";
            itemLookUpEdit.Name = "LookUpEditOutStoreType";
            itemLookUpEdit.Properties.ValueMember = "OutStoreCode";
            itemLookUpEdit.Properties.NullText = "";
            itemLookUpEdit.Properties.DataSource = WmsServiceFactory.GetServiceClient().GetOutStoreTypes();
        }

        public static void BindingLookUpEditSystemUser(LookUpEdit lookUpEdit, TextEdit valueTextEdit,
                                                       string propertyName)
        {
            var itemLookUpEdit = lookUpEdit;
            itemLookUpEdit.Properties.TextEditStyle = TextEditStyles.Standard;
            itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("USERID", "用户ID"),
                                                               new LookUpColumnInfo("USERNAME", "用户姓名")
                                                           });
            itemLookUpEdit.Properties.DisplayMember = "USERID";
            itemLookUpEdit.Name = "LookUpEditSystemUser";
            itemLookUpEdit.Properties.ValueMember = "USERID";
            itemLookUpEdit.Properties.NullText = "";
            itemLookUpEdit.Properties.DataSource = WmsServiceFactory.GetServiceClient().GetSystemUsers();

            itemLookUpEdit.EditValueChanging += (sender, e) =>
                                                    {
                                                        var systemUser =
                                                            (SystemUser)
                                                            itemLookUpEdit.Properties.GetDataSourceRowByKeyValue(
                                                                e.NewValue);
                                                        if (systemUser != null)
                                                        {
                                                            if (itemLookUpEdit.DataBindings.Count > 0)
                                                            {
                                                                var type =
                                                                    itemLookUpEdit.DataBindings[0].DataSource.GetType();
                                                                var pi = type.GetProperty(propertyName);
                                                                pi.SetValue(itemLookUpEdit.DataBindings[0].DataSource,
                                                                            systemUser.USERNAME, null);
                                                            }
                                                        }
                                                    };
        }

        public static void BindingLookUpEditPayType(LookUpEdit lookUpEdit, TextEdit textEditPayTypeName)
        {
            var itemLookUpEdit = lookUpEdit;
            itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("PayTypeCode", "付款方式代码"),
                                                               new LookUpColumnInfo("PayTypeName", "付款方式名称")
                                                           });
            itemLookUpEdit.Properties.DisplayMember = "PayTypeCode";
            itemLookUpEdit.Name = "LookUpEditPayType";
            itemLookUpEdit.Properties.ValueMember = "PayTypeCode";
            itemLookUpEdit.Properties.NullText = "";
            itemLookUpEdit.Properties.DataSource = WmsServiceFactory.GetServiceClient().GetPayTypes();

            itemLookUpEdit.EditValueChanging += (sender, e) =>
                                                    {
                                                        var payType =
                                                            (PayType)
                                                            itemLookUpEdit.Properties.GetDataSourceRowByKeyValue(
                                                                e.NewValue);
                                                        if (payType != null)
                                                        {
                                                            if (itemLookUpEdit.DataBindings.Count > 0)
                                                            {
                                                                var type =
                                                                    itemLookUpEdit.DataBindings[0].DataSource.GetType();
                                                                var pi = type.GetProperty("PayTypeName");
                                                                pi.SetValue(itemLookUpEdit.DataBindings[0].DataSource,
                                                                            payType.PayTypeName, null);
                                                            }
                                                            else
                                                            {
                                                                if (textEditPayTypeName != null)
                                                                {
                                                                    textEditPayTypeName.EditValue = payType.PayTypeName;
                                                                }
                                                            }
                                                        }
                                                    };
        }

        public static void BindingLookUpEditProvider2(LookUpEdit lookUpEdit, TextEdit valueTextEdit,
                                                      TextEdit openingBankTextEdit, TextEdit bankAccountTextEdit)
        {
            var itemLookUpEdit = lookUpEdit;
            itemLookUpEdit.Properties.TextEditStyle = TextEditStyles.Standard;
            itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("OWNERNO", "代码"),
                                                               new LookUpColumnInfo("OWNERNAME", "名称")
                                                           });
            itemLookUpEdit.Properties.DisplayMember = "OWNERNO";
            itemLookUpEdit.Name = "LookUpEditProvider2";
            itemLookUpEdit.Properties.ValueMember = "OWNERNO";
            itemLookUpEdit.Properties.NullText = "";
            itemLookUpEdit.Properties.DataSource = WmsServiceFactory.GetServiceClient().GetCustomers();

            itemLookUpEdit.EditValueChanging += (sender, e) =>
                                                    {
                                                        var customer =
                                                            (Customer)
                                                            itemLookUpEdit.Properties.GetDataSourceRowByKeyValue(
                                                                e.NewValue);
                                                        if (customer != null)
                                                        {
                                                            if (itemLookUpEdit.DataBindings.Count > 0)
                                                            {
                                                                var type =
                                                                    itemLookUpEdit.DataBindings[0].DataSource.GetType();
                                                                var pi = type.GetProperty("ProviderName");
                                                                pi.SetValue(itemLookUpEdit.DataBindings[0].DataSource,
                                                                            customer.OWNERNAME, null);

                                                                pi = type.GetProperty("OpeningBank");
                                                                pi.SetValue(itemLookUpEdit.DataBindings[0].DataSource,
                                                                            customer.OWNERBANK, null);

                                                                pi = type.GetProperty("BankAccount");
                                                                pi.SetValue(itemLookUpEdit.DataBindings[0].DataSource,
                                                                            customer.OWNERACCOUNT, null);
                                                            }
                                                        }
                                                    };
        }

        public static void BindingComboBoxSpecificationType(GridView gv, GridColumn gc, RepositoryItem comboBox)
        {
            var itemComboBox = (RepositoryItemComboBox) comboBox;
            itemComboBox.Items.Clear();
            var list = StaticObjects.GetSpecificationTypes();
            foreach (var obj in list)
            {
                itemComboBox.Items.Add(obj);
            }
            gv.GridControl.RepositoryItems.AddRange(
                new RepositoryItem[] {itemComboBox});
            gc.ColumnEdit = itemComboBox;
        }

        public static void BindingComboBoxSpecificationTypeItems(RepositoryItem comboBox, string newGoodsCode)
        {
            var itemComboBox = (RepositoryItemComboBox) comboBox;
            itemComboBox.Items.Clear();
            var list = StaticObjects.GetSpecificationTypeItems();
            var temp = list.FindAll((o) => o.NewGoodsCode == newGoodsCode);
            foreach (var obj in temp)
            {
                itemComboBox.Items.Add(obj.SpecificationType);
            }
        }
    }
}